package mest.controller.cxj;

import com.alibaba.fastjson.JSON;
import mest.VO.cxj.FinancialCostOrderVO;
import mest.VO.cxj.PlanCostorderVO;
import mest.entity.DictionariesOrder;
import mest.entity.FinancialCostOrder;
import mest.entity.PlanCostOrder;
import mest.entity.PlanCostOrderRecord;
import mest.service.cxj.DictionariesOrderService;
import mest.service.cxj.FinancialCostOrderService;
import mest.service.system.DBService;
import mest.util.NumberConst;
import mest.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("financialCostOrder")
@CrossOrigin
public class FinancialCostOrderController {

    @Autowired
    private FinancialCostOrderService financialCostorderService;

    @Autowired
    private DictionariesOrderService dictionariesOrderService;

    @Autowired
    private DBService dbService;

    /**
     * 生成财务费用单编码
     * @return
     */
    @GetMapping("getCode")
    private String getCode() {
        String theirDb = dbService.getCompanyDB();
        DictionariesOrder dictionariesOrder = dictionariesOrderService.selectDictionariesOrder(theirDb);
        int financialCostOrder = dictionariesOrder.getFinancialCostOrder();
        return NumberConst.FinancialCostOrder_No + "-" + financialCostOrder;
    }


    /**
     * 新增财务费用单记录
     * @param fco
     * @return
     */
    @PostMapping("insert")
    public String insert(@RequestBody FinancialCostOrder fco) {
        String theirDb = dbService.getCompanyDB();
        Integer userId = dbService.getUserId(); //获取操作人ID
        ResultObj resultObj = null;
        try {
            fco.setEfId(userId);//设置当前操作人
            fco.setTableNumber(getCode());   //写入财务费用单编码
            if (financialCostorderService.insertFinancialCostOrder(fco, theirDb) > 0) {
                dictionariesOrderService.updateDictionariesOrderByFinancialCostOrderNumber(theirDb);
                resultObj = ResultObj.builder().code(0).msg("添加成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("添加失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }


    /**
     * 修改状态为已完成
     *
     * @return
     */
    @PutMapping("changeStatus")
    public String changeStatus(@RequestParam Integer id) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        if (financialCostorderService.changeStatus(id, theirDb) > 0) {
            resultObj = ResultObj.builder().code(0).msg("更新成功").build();
        } else {
            resultObj = ResultObj.builder().code(-1).msg("更新失败").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 查询全部财务费用单
     * @param page
     * @param limit
     * @param status
     * @param content
     * @return
     */
    @GetMapping("selectAll")
    public String selectAll(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam Integer status,
                            @RequestParam(required = false) String content) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        try {
            List<FinancialCostOrderVO> planCostorderVOList = financialCostorderService.selectAllFinancialCostOrder(page, limit, status, content, theirDb);
            Integer count = financialCostorderService.selectCountFinancialCostOrder(status, content, theirDb);
            resultObj = new ResultObj(0, "success", count, planCostorderVOList);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 修改备注
     * @param fco
     * @return
     */
    @PutMapping("modify")
    public String modify(@RequestBody FinancialCostOrder fco) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        try {
            if (financialCostorderService.modifyFinancialCostOrder(fco, theirDb) > 0) {
                resultObj = ResultObj.builder().code(0).msg("修改成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("修改失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

}
